package moreThanHalfNum_solution

// 暴力法
func MoreThanHalfNum_Solution(numbers []int) int {
	curNum := 0
	for i := 0; i < len(numbers); i++ {
		count := 0
		for j := 0; j < len(numbers); j++ {
			curNum = numbers[i]
			if numbers[i] == numbers[j] {
				count++
				if count > len(numbers)/2 {
					return curNum
				}
			}
		}
	}
	return 0
}

func MoreThanHalfNum_Solution2(numbers []int) int {
	m := make(map[int]int, 1)
	if len(numbers) == 1 {
		return numbers[0]
	}
	for i := 0; i < len(numbers); i++ {
		if _, ok := m[numbers[i]]; ok {
			m[numbers[i]]++
			if m[numbers[i]] > len(numbers)/2 {
				return numbers[i]

			}
		} else {
			m[numbers[i]] = 1
		}

	}

	return 0

}
